package util

import (
	"encoding/json"
	"io"
	"net/http"
)

func Render(w http.ResponseWriter, code int, res interface{}, msg string) {
	finalRes := map[string]interface{}{
		"code":    code,
		"res":     res,
		"message": msg}
	resstr, err := json.Marshal(finalRes)
	if err != nil {
		io.WriteString(w, "结果序列化出错")
	}
	io.WriteString(w, string(resstr))
}

func SuccessRender(w http.ResponseWriter, res interface{}) {
	Render(w, 200, res, "success")
}

func SuccessRenderNoRes(w http.ResponseWriter) {
	Render(w, 200, "", "success")
}

func FailRender(w http.ResponseWriter, reason string) {
	Render(w, 500, "fail", reason)
}
